package com.hscm.boss.mvc.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hscm.boss.common.consants.SysConstants;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.convert.PaymentConvert;
import com.hscm.boss.convert.UserConvert;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.dao.entity.UserPayConfig;
import com.hscm.boss.dao.entity.UserPayInfo;
import com.hscm.boss.mvc.model.AjaxResult;
import com.hscm.boss.mvc.model.from.config.UserPayConfigForm;
import com.hscm.boss.mvc.model.pages.PageInfo;
import com.hscm.boss.mvc.model.vo.config.UserPayConfigVo;
import com.hscm.boss.mvc.model.vo.config.UserPayInfoVo;
import com.hscm.boss.mvc.model.vo.user.MatchUserInfoVo;
import com.hscm.boss.mvc.service.UserConfigService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserConfigController extends BaseController {

    @Resource
    private UserConfigService userConfigService;

    @Resource
    private PaymentConvert paymentConvert;

    @Resource
    private UserConvert userConvert;

    /*用戶支付產品分頁*/
    @GetMapping("/config/user/product")
    public AjaxResult pageList(@RequestParam(value = "pageNo") Integer pageNo) {
        pageNo = defPageNo(pageNo);
        IPage<UserPayConfig> pageResult = userConfigService.pageList(pageNo, SysConstants.PAGE_SIZE);


        //创建分页对象
        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), pageResult.getPages());

        //取得分页中的数据
        List<UserPayConfig> userConfigList = pageResult.getRecords();

        HashMap<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);

        //userConfig ===> userConfigVo
        List<UserPayConfigVo> listUserPayConfigVo = paymentConvert.toListUserPayConfigVo(userConfigList);
        data.put("page", pageInfo);
        data.put("list", listUserPayConfigVo);

        return AjaxResult.ok(data);

    }


    //模糊查询商户名称对象
    @GetMapping("/user/match/shopname")
    public AjaxResult matchShopName(@RequestParam(value = "shopName") String shopName) {
        if (StrUtil.isNotBlank(shopName) && shopName.trim().length() > 1) {

            List<UserInfo> userInfoList = userConfigService.matchShopName(shopName);
            List<MatchUserInfoVo> listUserInfoVo = userConvert.toListMatchUserInfoVo(userInfoList);
            return AjaxResult.ok(listUserInfoVo);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

    //创建商户支付配置
    @PostMapping("/config/user/creat")
    public AjaxResult matchShopName(@RequestBody @Valid UserPayConfigForm userPayConfigForm) {
        userConfigService.createUserPayConfig(userPayConfigForm);
        return AjaxResult.ok();
    }

    //查询支付第三方接口的配置信息
    @GetMapping("/config/user/payinfo")
    public AjaxResult queryPayInfo(@RequestParam String userNo, @RequestParam String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)) {
            List<UserPayInfo> payInfoList = userConfigService.queryPayInfo(userNo, productCode);
            List<UserPayInfoVo> listUserPayInfoVo = userConvert.toListUserPayInfoVo(payInfoList);
            return AjaxResult.ok(listUserPayInfoVo);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

    //删除用户支付配置
    @PostMapping("/config/user/remove")
    public AjaxResult removePayInfo(@RequestParam String userNo, @RequestParam String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)) {
            userConfigService.removePayInfoConfig(userNo, productCode);
            return AjaxResult.ok();

        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

}
